package com.gujiangbo.chapter16;

/**
 * @Author: gujiangbo.co
 * @Contact: 646832012@qq.com
 * @Date: 2022/4/10 21:30
 * @Version: 1.0
 * @Description: -XX:+PrintGCDetails
 */
public class JavaLocalVarGC {


    // 不回收，full gc 将buffer对象放入老年代
    //[GC (System.gc()) [PSYoungGen: 12861K->640K(76288K)] 12861K->10880K(251392K), 0.0082313 secs] [Times: user=0.02 sys=0.01, real=0.01 secs]
    //[Full GC (System.gc()) [PSYoungGen: 640K->0K(76288K)] [ParOldGen: 10240K->10634K(175104K)] 10880K->10634K(251392K), [Metaspace: 3029K->3029K(1056768K)], 0.0028625 secs] [Times: user=0.01 sys=0.00, real=0.00 secs]
    public void localVarGc1() {
        byte[] buffer = new byte[10 * 1024 * 1024];
        System.gc();
    }

    // 会触发GC  YoungGc 将对象从10M 回收到640k..
    //[GC (System.gc()) [PSYoungGen: 12861K->640K(76288K)] 12861K->640K(251392K), 0.0070979 secs] [Times: user=0.01 sys=0.00, real=0.01 secs]
    //[Full GC (System.gc()) [PSYoungGen: 640K->0K(76288K)] [ParOldGen: 0K->393K(175104K)] 640K->393K(251392K), [Metaspace: 3008K->3008K(1056768K)], 0.0039701 secs] [Times: user=0.01 sys=0.00, real=0.01 secs]
    public void localVarGc2() {
        byte[] buffer = new byte[10 * 1024 * 1024];//10M
        buffer = null;
        System.gc();
    }

    // 不会触发GC  Full gc 将对象放入老年代了
    //[GC (System.gc()) [PSYoungGen: 12861K->10736K(76288K)] 12861K->10792K(251392K), 0.0076756 secs] [Times: user=0.02 sys=0.01, real=0.01 secs]
    //[Full GC (System.gc()) [PSYoungGen: 10736K->0K(76288K)] [ParOldGen: 56K->10634K(175104K)] 10792K->10634K(251392K), [Metaspace: 3028K->3028K(1056768K)], 0.0074140 secs] [Times: user=0.01 sys=0.00, real=0.01 secs]
    public void localVarGc3() {
        {
            byte[] buffer = new byte[10 * 1024 * 1024];
        }
        System.gc();
    }

    // 会触发GC 局部变量表中buffer的索引将会被 v的索引覆盖，buffer将被回收。
    //[GC (System.gc()) [PSYoungGen: 12861K->640K(76288K)] 12861K->640K(251392K), 0.0062474 secs] [Times: user=0.01 sys=0.01, real=0.01 secs]
    //[Full GC (System.gc()) [PSYoungGen: 640K->0K(76288K)] [ParOldGen: 0K->395K(175104K)] 640K->395K(251392K), [Metaspace: 3045K->3045K(1056768K)], 0.0033820 secs] [Times: user=0.01 sys=0.00, real=0.00 secs]
    public void localVarGc4() {
        {
            byte[] buffer = new byte[10 * 1024 * 1024];
        }
        int v = 10;
        System.gc();
    }

    //会触发GC
    //[GC (System.gc()) [PSYoungGen: 12861K->640K(76288K)] 12861K->10880K(251392K), 0.0071479 secs] [Times: user=0.01 sys=0.01, real=0.00 secs]
    //[Full GC (System.gc()) [PSYoungGen: 640K->0K(76288K)] [ParOldGen: 10240K->10635K(175104K)] 10880K->10635K(251392K), [Metaspace: 3045K->3045K(1056768K)], 0.0033839 secs] [Times: user=0.01 sys=0.00, real=0.01 secs]
    //[GC (System.gc()) [PSYoungGen: 0K->0K(76288K)] 10635K->10635K(251392K), 0.0004337 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
    //[Full GC (System.gc()) [PSYoungGen: 0K->0K(76288K)] [ParOldGen: 10635K->376K(175104K)] 10635K->376K(251392K), [Metaspace: 3045K->3045K(1056768K)], 0.0040606 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
    public void localVarGc5() {
        localVarGc1();
        System.gc();
    }


    public static void main(String[] args) {
        JavaLocalVarGC javaLocalVarGc = new JavaLocalVarGC();
        javaLocalVarGc.localVarGc5();
    }

}
